package com.bdqn.hospital.controller;

import com.alibaba.fastjson.JSON;
import com.bdqn.hospital.pojo.Patient;
import com.bdqn.hospital.service.impl.PatientServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @version : 1.0
 * @Author :guogd
 * @createTime: 2023/03/01 14:35
 * @Description: com.bdqn.hospital.controller
 */
@Controller
@RequestMapping("patient")
public class PatientController {

    @Autowired
    private PatientServiceImpl patientService;

    /**
     * 病人表Controller
     */

    @ResponseBody
    @RequestMapping("checkAddPatient")
    public Integer getInfoByIdCard(String idCard,String idMedicare){
            return patientService.checkAddPatient(idCard,idMedicare);
    }

    @RequestMapping("addPatient")
    public void addPatient(Patient patient, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=UTF-8");
        if(patient.getIdMedicare()!=""){
            patient.setIsMedicare(2);
        }else{
            patient.setIsMedicare(1);
        }
        Integer row = patientService.addPatient(patient);
        if(row>0){
            resp.getWriter().print("<script>alert('创建就诊卡成功！');location.href='/registry/toRegistry';</script>");
        }else{
            resp.getWriter().print("<script>alert('创建就诊卡失败！');history.back();</script>");
        }
    }
    @ResponseBody
    @RequestMapping("getPatientByIdCard")
    public String getPatientByIdCard(String idCard){
            return JSON.toJSONString(patientService.getPatientByIdCard(idCard));
    }
}
